package com.gxa.myshiro.realm;

import com.gxa.myshiro.pojo.Access;
import com.gxa.myshiro.pojo.Role;
import com.gxa.myshiro.pojo.User;
import com.gxa.myshiro.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.apache.tomcat.util.net.openssl.ciphers.Authentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @program:
 * @description:
 * @author: Allen
 * @create: 2019/7/29 19:40
 **/
@Component("myRealm")
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    /**
     * 授权的方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //得到用户名字
        String userName = (String) principalCollection.getPrimaryPrincipal();
        User user = userService.getUser(userName);

        //通过用户来获取角色信息
        Set<String> roles = getRolesByUser(user);
        //通过用户来获取角色权限
        Set<String> access = getAccessByUser(user);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //设置角色和权限数据
        authorizationInfo.setRoles(roles);
        authorizationInfo.setStringPermissions(access);

        return authorizationInfo;
    }

    /**
     * 获取权限列表
     * @param user
     * @return
     */
    private Set<String> getAccessByUser(User user) {
        if (user == null){
            return null;
        }
        Role role = user.getRole();

        if (role==null){
            return null;
        }
        List<Access> accessList = role.getAccessList();
        if (accessList == null || accessList.isEmpty()){
            return null;
        }
        Set<String> accesses = new HashSet<>();
        for (Access access : accessList) {
            accesses.add(access.getAccessName());
        }
        return accesses;
    }

    /**
     * 获取角色数据
     * @param user
     * @return
     */
    private Set<String> getRolesByUser(User user) {
        if (user == null){
            return null;
        }
        Role role = user.getRole();

        if (role==null){
            return null;
        }

        HashSet<String> roles = new HashSet<>();
        roles.add(role.getRoleName());

        return roles;
    }


    /**
     * 认证的方法
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取用户名字
        String userName = (String) authenticationToken.getPrincipal();
        //通过用户名获取密码
        String password = getPwdByUserName(userName);

        if (password == null){
            return  null;
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName,password,"myRealm");
        info.setCredentialsSalt(ByteSource.Util.bytes("czg"));
        return info;
    }

    /**
     * 根据用户名获取密码的方法
     * @param userName
     * @return
     */
    private String getPwdByUserName(String userName) {

        User user = userService.getUser(userName);
        if (user == null){
            return null;
        }
        return user.getUserPwd();
    }
}
